/**
 * 
 */
package com.changer.common.service.impl;

import java.io.File;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.annotation.Resource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.mail.javamail.JavaMailSenderImpl;

import com.changer.common.service.MailService;
import com.changer.framework.util.StringUtils;

/**
 * @author Jiwei.guo
 *
 */
public class MailServiceImpl implements MailService{
	
	private static final Logger LOGGER = LoggerFactory.getLogger(MailServiceImpl.class);
	
	private static final String DEFAULT_MAIL_ACCOUNT = "webmaster@changer.hk";
	
	private static final String DEFAULT_MAIL_ALIAS = "成者云";
	
	@Resource
	private JavaMailSenderImpl mailSender;
	
	@Resource(name="mainConfig")
	private Properties props;
	
	private String mailFrom;
	
	public void send(String alias, String[] tos, String subject, String content) throws Exception{
		MimeMessage  message = this.mailSender.createMimeMessage();
		message.setFrom(new InternetAddress(this.getMailAccount(), this.getAlias(alias)));
		message.setSubject(subject);
		message.setContent(content, "text/html;charset=utf-8");
		for(String to : tos){
			message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
		}
		LOGGER.debug("sending email : " + message);
		this.mailSender.send(message);
	}
	
	public void send(String alias, String[] tos, String subject, String content, File[] attachments) throws Exception{
		MimeMessage  message = this.mailSender.createMimeMessage();
		message.setFrom(new InternetAddress(this.getMailAccount(), this.getAlias(alias)));
		message.setSubject(subject);
		//
		Multipart multipart = new MimeMultipart();
		
		BodyPart contentPart = new MimeBodyPart();
		contentPart.setText(content);
		multipart.addBodyPart(contentPart);
		//
		for(File file : attachments){
			BodyPart attachmentPart= new MimeBodyPart();
			DataSource source = new FileDataSource(file.getAbsolutePath());
			attachmentPart.setDataHandler(new DataHandler(source));
			attachmentPart.setFileName(MimeUtility.encodeText(file.getName()));
			multipart.addBodyPart(attachmentPart);
		}
        message.setContent(multipart);
		for(String to : tos){
			message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
		}
		LOGGER.debug("sending email : " + message);
		this.mailSender.send(message);
	}
	
	private String getMailAccount(){
		if(StringUtils.isEmpty(this.mailFrom)){
			this.mailFrom = this.mailSender.getUsername();
		}
		if(StringUtils.isEmpty(this.mailFrom)){
			this.mailFrom = DEFAULT_MAIL_ACCOUNT;
		}
		return this.mailFrom;
	}
	
	private String getAlias(String alias){
		if(!StringUtils.isEmpty(alias)){
			return alias;
		}
		return this.props.getProperty("mail_alias", DEFAULT_MAIL_ALIAS);
	}
}
